package com.dailyblue.java.mybatis.mybatis03.test;

import com.dailyblue.java.mybatis.mybatis03.bean.Dept;
import com.dailyblue.java.mybatis.mybatis03.bean.Emp;
import com.dailyblue.java.mybatis.mybatis03.mapper.DeptMapper;
import com.dailyblue.java.mybatis.mybatis03.mapper.EmpMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Set;

public class App {

    private static SqlSession session;
    private static EmpMapper empMapper;
    private static DeptMapper deptMapper;

    static {
        try {
            InputStream in = Resources.getResourceAsStream("config.xml");
            SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(in);
            session = ssf.openSession();
            empMapper = session.getMapper(EmpMapper.class);
            deptMapper = session.getMapper(DeptMapper.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void a() {
        List<Emp> list = empMapper.findAllEmp();
        list.forEach(System.out::println);
    }

    private static void b() {
        List<Emp> list = empMapper.findAllEmp2();
        // 组装
        list.forEach(emp -> {
            emp.setDept(deptMapper.findDeptByNo(emp.getDeptNo()));
        });
        list.forEach(System.out::println);
    }

    private static void c(){
        Dept dept = deptMapper.findDeptByNo2(10);
        System.out.println(dept);
        dept.getEmps().forEach(System.out::println);
    }

    private static void d(){
        // 只能查询出来10号部门
        Dept dept = deptMapper.findDeptByNo(10);
        // 查询该部门所有的员工信息
        Set<Emp> emps = empMapper.findAllEmpByDeptNo(dept.getDeptNo());
        dept.setEmps(emps);
        System.out.println(dept);
        dept.getEmps().forEach(System.out::println);

    }
    public static void main(String[] args) {
        d();
    }
}
